//
//  MNewFeatureCell.swift
//  iOSweibo
//
//  Created by lzm on 16/5/15.
//  Copyright © 2016年 lzm. All rights reserved.
//

import UIKit

class MNewFeatureCell: UICollectionViewCell {
    
    //根据imgIndex显示对应的图片
    var imgIndex : Int = 0 {
        didSet{
            backgroundImage.image = UIImage(named: "new_feature_\(imgIndex + 1)")
            startButton.hidden = true
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //加载背景图片
        prepareUI()
    }
    //加载背景图片
    private func prepareUI()
    {
        //懒加载控件添加到contenView
        contentView.addSubview(backgroundImage)
        contentView.addSubview(startButton)
        
        
        //添加约束
        backgroundImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }
    }
    
    //MARK: 添加动画
    func startBtnAnimation(){
        startButton.hidden = false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        //动画
        UIView.animateWithDuration(1, delay: 0.05, usingSpringWithDamping: 0.6, initialSpringVelocity: 1, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                MPrint(items: "动画完成")
        }
    }
    
    /// 开始按钮绑定时间
    func startBtn()
    {
        let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
        appDelegate?.switchRootVC(MtabBarController())
        
    }
    //懒加载背景图片
    private lazy var backgroundImage = UIImageView()
    
    //添加按钮
    private lazy var startButton : UIButton = {
        let button  =  UIButton()
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.sizeToFit()
        //添加按钮绑定时间
        button.addTarget(self, action: "startBtn", forControlEvents: UIControlEvents.TouchUpInside)
        button.hidden = true
        
        return button
    }()
    
    
}
